/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.Ghe;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author PC
 */
public class GheDAO {
     public static boolean Them(Ghe n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO `quanlyduongsat`.`ghe` (`so_hieu`, `toa`, `tinh_trang`, `gia`) VALUES   ('%s','%s',%d,%f);",n.SoHieu,n.Toa,n.TinhTrang,n.Gia);
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
           // JOptionPane.showMessageDialog(null, "Thêm Không thành công!!!", "Thông báo", 1);
        }
        return false;
    }

    public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from Ghe where so_hieu = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select ghe.SO_HIEU,t.id,t.ten_tau,toa.SO_HIEU,tt.TEN,GHE.GIA  from tau t,TOA toa, GHE ghe, TINH_TRANG_GHE tt where ghe.TOA=toa.SO_HIEU and ghe.TINH_TRANG=tt.ID and t.id=toa.tau";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(String MaNV)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From ghe Where MaNV like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, MaNV);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
       public static boolean Update(Ghe NV,String id, String toa)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Update ghe set toa=?, tinh_trang=? ,gia = ? Where so_hieu = ? and toa=?";           
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, NV.Toa);
            ps.setInt(2, NV.TinhTrang);
            ps.setDouble(3, NV.Gia);
            ps.setString(4,id);
              ps.setString(5,toa);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
            System.err.println(ex.getMessage());
        }
        return false;
    }
}
